home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 November / PCWNOV07.iso / Software / Freeware / NSIS 2.29 / nsis-2.29-setup.exe / Include / Memento.nsh < prev    next >
Encoding:
Text File  |  2007-03-10  |  10.3 KB  |  525 lines

  1. !verbose push
  2. !verbose 3
  3.  
  4. !include LogicLib.nsh
  5. !include Sections.nsh
  6.  
  7. !ifndef ___MEMENTO_NSH___
  8. !define ___MEMENTO_NSH___
  9.  
  10. #####################################
  11. ### Memento                       ###
  12. #####################################
  13.  
  14. /*
  15.  
  16. Memento is a set of macros that allow installers to remember user selection
  17. across separate runs of the installer. Currently, it can remember the state
  18. of sections and mark new sections as bold. In the future, it'll integrate
  19. InstallOptions and maybe even the Modern UI.
  20.  
  21. A usage example can be found in `Examples\Memento.nsi`.
  22.  
  23. */
  24.  
  25. #####################################
  26. ### Usage Instructions            ###
  27. #####################################
  28.  
  29. /*
  30.  
  31. 1. Declare usage of Memento by including Memento.nsh at the top of the script.
  32.  
  33.       !include Memento.nsh
  34.  
  35. 2. Define MEMENTO_REGISTRY_ROOT and MEMENTO_REGISTRY_KEY with the a registry key
  36.    where sections' state should be saved.
  37.  
  38.       !define MEMENTO_REGISTRY_ROOT HKLM
  39.       !define MEMENTO_REGISTRY_KEY \
  40.                 Software\Microsoft\Windows\CurrentVersion\Uninstall\MyProgram
  41.  
  42. 3. Replace Section with ${MementoSection} and SectionEnd with ${MementoSectionEnd}
  43.    for sections that whose state should be remembered by Memento.
  44.  
  45.    For sections that should be unselected by default, use ${MementoSection}'s
  46.    brother - ${MementoUnselectedSection}.
  47.  
  48.    Sections that don't already have an identifier must be assigned one.
  49.  
  50.    Section identifiers must stay the same across different versions of the
  51.    installer or their state will be forgotten.
  52.  
  53. 4. Add a call to ${MementoSectionRestore} to .onInit to restore the state
  54.    of all sections from the registry.
  55.  
  56.       Function .onInit
  57.  
  58.         ${MementoSectionRestore}
  59.  
  60.       FunctionEnd
  61.  
  62. 5. Add a call to ${MementoSectionSave} to .onInstSuccess to save the state
  63.    of all sections to the registry.
  64.  
  65.       Function .onInstSuccess
  66.  
  67.         ${MementoSectionSave}
  68.  
  69.       FunctionEnd
  70.  
  71. 6. Tattoo the location of the chosen registry key on your arm.
  72.  
  73. */
  74.  
  75. #####################################
  76. ### User API                      ###
  77. #####################################
  78.  
  79. ;
  80. ; ${MementoSection}
  81. ;
  82. ;   Defines a section whose state is remembered by Memento.
  83. ;
  84. ;   Usage is similar to Section.
  85. ;
  86. ;     ${MementoSection} "name" "some_id"
  87. ;
  88.  
  89. !define MementoSection "!insertmacro MementoSection"
  90.  
  91. ;
  92. ; ${MementoSectionEnd}
  93. ;
  94. ;   Ends a section previously opened using ${MementoSection}.
  95. ;
  96. ;   Usage is similar to SectionEnd.
  97. ;
  98. ;     ${MementoSection} "name" "some_id"
  99. ;        # some code...
  100. ;     ${MementoSectionEnd}
  101. ;
  102.  
  103. ;
  104. ; ${MementoUnselectedSection}
  105. ;
  106. ;   Defines a section whose state is remembered by Memento and is
  107. ;   unselected by default.
  108. ;
  109. ;   Usage is similar to Section with the /o switch.
  110. ;
  111. ;     ${MementoUnselectedSection} "name" "some_id"
  112. ;
  113.  
  114. !define MementoUnselectedSection "!insertmacro MementoUnselectedSection"
  115.  
  116. ;
  117. ; ${MementoSectionEnd}
  118. ;
  119. ;   Ends a section previously opened using ${MementoSection}.
  120. ;
  121. ;   Usage is similar to SectionEnd.
  122. ;
  123. ;     ${MementoSection} "name" "some_id"
  124. ;        # some code...
  125. ;     ${MementoSectionEnd}
  126. ;
  127.  
  128. !define MementoSectionEnd "!insertmacro MementoSectionEnd"
  129.  
  130. ;
  131. ; ${MementoSectionDone}
  132. ;
  133. ;   Used after all ${MementoSection} have been set.
  134. ;
  135. ;     ${MementoSection} "name1" "some_id1"
  136. ;        # some code...
  137. ;     ${MementoSectionEnd}
  138. ;
  139. ;     ${MementoSection} "name2" "some_id2"
  140. ;        # some code...
  141. ;     ${MementoSectionEnd}
  142. ;
  143. ;     ${MementoSection} "name3" "some_id3"
  144. ;        # some code...
  145. ;     ${MementoSectionEnd}
  146. ;
  147. ;     ${MementoSectionDone}
  148. ;
  149.  
  150. !define MementoSectionDone "!insertmacro MementoSectionDone"
  151.  
  152. ;
  153. ; ${MementoSectionRestore}
  154. ;
  155. ;   Restores the state of all Memento sections from the registry.
  156. ;
  157. ;   Commonly used in .onInit.
  158. ;
  159. ;     Function .onInit
  160. ;
  161. ;       ${MementoSectionRestore}
  162. ;
  163. ;     FunctionEnd
  164. ;
  165.  
  166. !define MementoSectionRestore "!insertmacro MementoSectionRestore"
  167.  
  168. ;
  169. ; ${MementoSectionSave}
  170. ;
  171. ;   Saves the state of all Memento sections to the registry.
  172. ;
  173. ;   Commonly used in .onInstSuccess.
  174. ;
  175. ;     Function .onInstSuccess
  176. ;
  177. ;       ${MementoSectionSave}
  178. ;
  179. ;     FunctionEnd
  180. ;
  181.  
  182. !define MementoSectionSave "!insertmacro MementoSectionSave"
  183.  
  184.  
  185. #####################################
  186. ### Internal Defines              ###
  187. #####################################
  188.  
  189. !define __MementoSectionIndex 1
  190.  
  191. #####################################
  192. ### Internal Macros               ###
  193. #####################################
  194.  
  195. !macro __MementoCheckSettings
  196.  
  197.   !ifndef MEMENTO_REGISTRY_ROOT | MEMENTO_REGISTRY_KEY
  198.  
  199.     !error "MEMENTO_REGISTRY_ROOT and MEMENTO_REGISTRY_KEY must be defined before using any of Memento's macros"
  200.  
  201.   !endif
  202.  
  203. !macroend
  204.  
  205. !macro __MementoSection flags name id
  206.  
  207.   !insertmacro __MementoCheckSettings
  208.  
  209.   !ifndef __MementoSectionIndex
  210.  
  211.     !error "MementoSectionDone already used!"
  212.  
  213.   !endif
  214.  
  215.   !define __MementoSectionLastSectionId `${id}`
  216.  
  217.   !verbose pop
  218.  
  219.   Section ${flags} `${name}` `${id}`
  220.  
  221.   !verbose push
  222.   !verbose 3
  223.  
  224. !macroend
  225.  
  226. #####################################
  227. ### User Macros                   ###
  228. #####################################
  229.  
  230. !macro MementoSection name id
  231.  
  232.   !verbose push
  233.   !verbose 3
  234.  
  235.   !insertmacro __MementoSection "" `${name}` `${id}`
  236.  
  237.   !verbose pop
  238.  
  239. !macroend
  240.  
  241. !macro MementoUnselectedSection name id
  242.  
  243.   !verbose push
  244.   !verbose 3
  245.  
  246.   !insertmacro __MementoSection /o `${name}` `${id}`
  247.  
  248.   !define __MementoSectionUnselected
  249.  
  250.   !verbose pop
  251.  
  252. !macroend
  253.  
  254. !macro MementoSectionEnd
  255.  
  256.   SectionEnd
  257.  
  258.   !verbose push
  259.   !verbose 3
  260.  
  261.   !insertmacro __MementoCheckSettings
  262.  
  263.   !ifndef __MementoSectionIndex
  264.  
  265.     !error "MementoSectionDone already used!"
  266.  
  267.   !endif
  268.  
  269.   !define /MATH __MementoSectionIndexNext \
  270.       ${__MementoSectionIndex} + 1
  271.  
  272.   Function __MementoSectionMarkNew${__MementoSectionIndex}
  273.  
  274.     ClearErrors
  275.     ReadRegDWORD $0 ${MEMENTO_REGISTRY_ROOT} `${MEMENTO_REGISTRY_KEY}` `MementoSection_${__MementoSectionLastSectionId}`
  276.  
  277.     ${If} ${Errors}
  278.  
  279.       !insertmacro SetSectionFlag `${${__MementoSectionLastSectionId}}` ${SF_BOLD}
  280.  
  281.     ${EndIf}
  282.  
  283.     GetFunctionAddress $0 __MementoSectionMarkNew${__MementoSectionIndexNext}
  284.     Goto $0
  285.  
  286.   FunctionEnd
  287.  
  288.   Function __MementoSectionRestoreStatus${__MementoSectionIndex}
  289.  
  290.     ClearErrors
  291.     ReadRegDWORD $0 ${MEMENTO_REGISTRY_ROOT} `${MEMENTO_REGISTRY_KEY}` `MementoSection_${__MementoSectionLastSectionId}`
  292.  
  293.     !ifndef __MementoSectionUnselected
  294.  
  295.       ${If} ${Errors}
  296.       ${OrIf} $0 != 0
  297.  
  298.         !insertmacro SelectSection `${${__MementoSectionLastSectionId}}`
  299.  
  300.       ${Else}
  301.  
  302.         !insertmacro UnselectSection `${${__MementoSectionLastSectionId}}`
  303.  
  304.       ${EndIf}
  305.  
  306.     !else
  307.  
  308.       !undef __MementoSectionUnselected
  309.  
  310.       ${If} ${Errors}
  311.       ${OrIf} $0 == 0
  312.  
  313.         !insertmacro UnselectSection `${${__MementoSectionLastSectionId}}`
  314.  
  315.       ${Else}
  316.  
  317.         !insertmacro SelectSection `${${__MementoSectionLastSectionId}}`
  318.  
  319.       ${EndIf}
  320.  
  321.     !endif
  322.  
  323.     GetFunctionAddress $0 __MementoSectionRestoreStatus${__MementoSectionIndexNext}
  324.     Goto $0
  325.  
  326.   FunctionEnd
  327.  
  328.   Function __MementoSectionSaveStatus${__MementoSectionIndex}
  329.  
  330.     ${If} ${SectionIsSelected} `${${__MementoSectionLastSectionId}}`
  331.  
  332.       WriteRegDWORD ${MEMENTO_REGISTRY_ROOT} `${MEMENTO_REGISTRY_KEY}` `MementoSection_${__MementoSectionLastSectionId}` 1
  333.  
  334.     ${Else}
  335.  
  336.       WriteRegDWORD ${MEMENTO_REGISTRY_ROOT} `${MEMENTO_REGISTRY_KEY}` `MementoSection_${__MementoSectionLastSectionId}` 0
  337.  
  338.     ${EndIf}
  339.  
  340.     GetFunctionAddress $0 __MementoSectionSaveStatus${__MementoSectionIndexNext}
  341.     Goto $0
  342.  
  343.   FunctionEnd
  344.  
  345.   !undef __MementoSectionIndex
  346.   !define __MementoSectionIndex ${__MementoSectionIndexNext}
  347.   !undef __MementoSectionIndexNext
  348.  
  349.   !undef __MementoSectionLastSectionId
  350.  
  351.   !verbose pop
  352.  
  353. !macroend
  354.  
  355. !macro MementoSectionDone
  356.  
  357.   !verbose push
  358.   !verbose 3
  359.  
  360.   !insertmacro __MementoCheckSettings
  361.  
  362.   Function __MementoSectionMarkNew${__MementoSectionIndex}
  363.   FunctionEnd
  364.  
  365.   Function __MementoSectionRestoreStatus${__MementoSectionIndex}
  366.   FunctionEnd
  367.  
  368.   Function __MementoSectionSaveStatus${__MementoSectionIndex}
  369.   FunctionEnd
  370.  
  371.   !undef __MementoSectionIndex
  372.  
  373.   !verbose pop
  374.  
  375. !macroend
  376.  
  377. !macro MementoSectionRestore
  378.  
  379.   !verbose push
  380.   !verbose 3
  381.  
  382.   !insertmacro __MementoCheckSettings
  383.  
  384.   Push $0
  385.   Push $1
  386.   Push $2
  387.   Push $3
  388.  
  389.     # check for first usage
  390.  
  391.     ClearErrors
  392.  
  393.     ReadRegStr $0 ${MEMENTO_REGISTRY_ROOT} `${MEMENTO_REGISTRY_KEY}` MementoSectionUsed
  394.  
  395.     ${If} ${Errors}
  396.  
  397.       # use script defaults on first run
  398.       Goto done
  399.  
  400.     ${EndIf}
  401.  
  402.     # mark new components in bold
  403.     
  404.     Call __MementoSectionMarkNew1
  405.  
  406.     # mark section groups in bold
  407.  
  408.     StrCpy $0 0
  409.     StrCpy $1 ""
  410.     StrCpy $2 ""
  411.     StrCpy $3 ""
  412.  
  413.     loop:
  414.  
  415.       ClearErrors
  416.  
  417.       ${If} ${SectionIsBold} $0
  418.  
  419.         ${If} $1 != ""
  420.  
  421.           !insertmacro SetSectionFlag $1 ${SF_BOLD}
  422.  
  423.         ${EndIf}
  424.  
  425.         ${If} $2 != ""
  426.  
  427.           !insertmacro SetSectionFlag $2 ${SF_BOLD}
  428.  
  429.         ${EndIf}
  430.  
  431.         ${If} $3 != ""
  432.  
  433.           !insertmacro SetSectionFlag $3 ${SF_BOLD}
  434.  
  435.         ${EndIf}
  436.  
  437.       ${ElseIf} ${Errors}
  438.  
  439.         Goto loop_end
  440.  
  441.       ${EndIf}
  442.  
  443.       ${If} ${SectionIsSectionGroup} $0
  444.  
  445.         ${If} $1 == ""
  446.  
  447.           StrCpy $1 $0
  448.  
  449.         ${ElseIf} $2 == ""
  450.  
  451.           StrCpy $2 $0
  452.  
  453.         ${ElseIf} $3 == ""
  454.  
  455.           StrCpy $3 $0
  456.  
  457.         ${EndIf}
  458.  
  459.       ${EndIf}
  460.  
  461.       ${If} ${SectionIsSectionGroupEnd} $0
  462.  
  463.         ${If} $3 != ""
  464.  
  465.           StrCpy $3 ""
  466.  
  467.         ${ElseIf} $2 != ""
  468.  
  469.           StrCpy $2 ""
  470.  
  471.         ${ElseIf} $1 != ""
  472.  
  473.           StrCpy $1 ""
  474.  
  475.         ${EndIf}
  476.  
  477.       ${EndIf}
  478.  
  479.       IntOp $0 $0 + 1
  480.  
  481.     Goto loop
  482.     loop_end:
  483.  
  484.     # restore sections' status
  485.  
  486.     Call __MementoSectionRestoreStatus1
  487.  
  488.   # all done
  489.  
  490.   done:
  491.  
  492.   Pop $3
  493.   Pop $2
  494.   Pop $1
  495.   Pop $0
  496.  
  497.   !verbose pop
  498.  
  499. !macroend
  500.  
  501. !macro MementoSectionSave
  502.  
  503.   !verbose push
  504.   !verbose 3
  505.  
  506.   !insertmacro __MementoCheckSettings
  507.  
  508.   Push $0
  509.  
  510.     WriteRegStr ${MEMENTO_REGISTRY_ROOT} `${MEMENTO_REGISTRY_KEY}` MementoSectionUsed ""
  511.   
  512.     Call __MementoSectionSaveStatus1
  513.  
  514.   Pop $0
  515.  
  516.   !verbose pop
  517.  
  518. !macroend
  519.  
  520.  
  521.  
  522. !endif # ___MEMENTO_NSH___
  523.  
  524. !verbose pop
  525.